package com.example.libowu.musicplayer;

import android.Manifest;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Build;
import android.os.IBinder;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;
import android.support.design.widget.NavigationView;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.view.menu.MenuView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.RemoteViews;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements onMusicList,View.OnClickListener,searchOnclick{
    static List<Music> list=new ArrayList<>();
    private RecyclerView recyclerView;
    private MusicAdapter musicAdapter;
    private String songN;
    private String authorN;
    private String songImg;
    private Uri songImgPath;
    private String songPath;
    private int songId;
    static MediaPlayer mediaPlayer;
    private TextView displayName;
    private ImageButton isStart;
    private ImageButton isLove;
    private ImageView musicImage;
    private LinearLayout contral;
    private String sendName;
    static int sendPosition;
    private ServiceConnection connection;
    private EditText search_bar;
    private RecyclerView searchContent;
    private List<searcharMusic> searcharMusics=new ArrayList<>();
    private DrawerLayout drawerLayout;
    private LinearLayout setTheme;
    private LinearLayout setTime;
    private NavigationView nav;
    private MusicChange musicChange;
    private LinearLayout sz;//遮罩层
    private FrameLayout all;
    private LinearLayout setGreen;
    private LinearLayout setPink;
    private LinearLayout setPaper;
    private LinearLayout setBlue;
    private LinearLayout setRed;
    private RelativeLayout re;
    private int backgroundColor;
    private TextView set_ten,set_two_ten,set_three_ten,set_four_ten,set_five_ten,set_six_ten;
    private MyserveForSetTime.Time myserveTime;
    private long finishTime;
    private Intent launchMyserve;
    private Exit exit;
    private ServiceConnection exitContent;
    private Button cancel;
    static PlayServer.StartPlay startPlay;
    static ServiceConnection startMusic;
    static int launth;
    static String songName;
    private musicProcess timeProcess;

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findView();
        cheakPermission();
        initInfo();
        init();
        //setTranslucentStatus(true);
        //hindBar();
        searchBar();
        loadTheme();
    }

    private void loadTheme() {
        LayoutInflater layout=this.getLayoutInflater();
        View view=layout.inflate(R.layout.heand, null);
        RelativeLayout relativeLayout=(RelativeLayout) view.findViewById(R.id.head);
        TextView textView=(TextView) view.findViewById(R.id.name);

        SharedPreferences sharedPreferences=getSharedPreferences("setTheme",AppCompatActivity.MODE_PRIVATE);
        backgroundColor=sharedPreferences.getInt("setTheme",R.drawable.background);
        all.setBackgroundResource(backgroundColor);
        drawerLayout.setBackgroundResource(backgroundColor);
        re.setBackgroundResource(backgroundColor);
        relativeLayout.setBackgroundResource(backgroundColor);
        cancel.setBackgroundResource(backgroundColor);

    }

    private void searchBar() {
        search_bar.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                String content=search_bar.getText().toString();
                if (content.length()==0){
                    searchContent.setVisibility(View.GONE);
                }else {
                    searcharMusics.clear();
                    for(int a=0;a<list.size();a++){
                        //Log.e("搜索起作用了",""+content);
                        if(list.get(a).getSongN().contains(content)){
                            /*(String songN,String authorN,Uri songImgPath,String songPath,int songId)*/
                            searchContent.setVisibility(View.VISIBLE);
                            searcharMusics.add(new searcharMusic(list.get(a).getSongN(),list.get(a).getAuthorN(),list.get(a).getSongImgPath(),list.get(a).getSongPath(),list.get(a).getSongId()));
                        }
                    }
                    searcharMusiAdapter sm=new searcharMusiAdapter(searcharMusics,MainActivity.this);
                    LinearLayoutManager llm=new LinearLayoutManager(MainActivity.this);
                    searchContent.setLayoutManager(llm);
                    searchContent.setAdapter(sm);
                   // Myservier.seekBar=MusicInterface.seekBar;
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }


    private void initInfo() {
        ++launth;
        if(launth<=1){
            musicChange=new MusicChange();

            IntentFilter intentFilter=new IntentFilter();
            intentFilter.addAction("MUSIC_CHANGE");
            registerReceiver(musicChange,intentFilter);

            exit=new Exit();
            IntentFilter intentFilter1=new IntentFilter();
            intentFilter1.addAction("TIME_UP");
            registerReceiver(exit,intentFilter1);


            SharedPreferences preferences=getSharedPreferences("lastSong",AppCompatActivity.MODE_PRIVATE);
            int position=preferences.getInt("position",0);
            Glide.with(this).load(list.get(position).getSongImgPath()).into(musicImage);
            displayName.setText(list.get(position).getSongN());
            sendName=list.get(position).getSongN();
            sendPosition=position;
            mediaPlayer=new MediaPlayer();
            try {
                mediaPlayer.setDataSource(list.get(position).getSongPath());
                mediaPlayer.prepare();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    private void cheakPermission() {
        if(ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED){
            ActivityCompat.requestPermissions(MainActivity.this,new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE},1);
        }else {
            musicList();
        }
        if(ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS)!=PackageManager.PERMISSION_GRANTED){
            ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS},2);
        }else {
            musicList();
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode){
            case 1:
                if(grantResults.length>0 && grantResults[0]==PackageManager.PERMISSION_GRANTED){
                    Toast.makeText(MainActivity.this,"权限以获取",Toast.LENGTH_SHORT).show();
                    musicList();
                }else {
                    Toast.makeText(MainActivity.this,"软件无法正常使用",Toast.LENGTH_SHORT).show();
                    finish();
                }
                break;
            case 2:
                if(grantResults.length>0 && grantResults[0]==PackageManager.PERMISSION_GRANTED){
                    musicList();
                }else {
                    Toast.makeText(MainActivity.this,"软件无法正常使用",Toast.LENGTH_SHORT).show();
                    finish();
                }
                break;
            default:
                break;
        }
    }

    private void findView() {

        recyclerView=(RecyclerView) findViewById(R.id.music_list);
        musicImage=(ImageView) findViewById(R.id.music_image);
        displayName=(TextView) findViewById(R.id.display_name);
        isStart=(ImageButton) findViewById(R.id.is_start);
        isLove=(ImageButton) findViewById(R.id.is_love);
        contral=(LinearLayout) findViewById(R.id.contral);
        search_bar=(EditText) findViewById(R.id.search_bar);
        searchContent=(RecyclerView) findViewById(R.id.search_content);
        drawerLayout=(DrawerLayout) findViewById(R.id.draw);
        setTheme=(LinearLayout) findViewById(R.id.set_theme);
        setTime=(LinearLayout) findViewById(R.id.set_time);
        nav=(NavigationView) findViewById(R.id.nav);
        sz=(LinearLayout) findViewById(R.id.sz);
        setGreen=(LinearLayout) findViewById(R.id.set_green);
        setBlue=(LinearLayout) findViewById(R.id.set_blue);
        setPaper=(LinearLayout) findViewById(R.id.set_parper);
        setRed=(LinearLayout) findViewById(R.id.set_red);
        setPink=(LinearLayout) findViewById(R.id.set_pink);
        all=(FrameLayout) findViewById(R.id.all);
        re=(RelativeLayout) findViewById(R.id.re);
        set_five_ten=(TextView) findViewById(R.id.set_five_ten);
        set_six_ten=(TextView) findViewById(R.id.set_six_ten);
        set_four_ten=(TextView) findViewById(R.id.set_four_ten);
        set_three_ten=(TextView) findViewById(R.id.set_three_ten);
        set_two_ten=(TextView) findViewById(R.id.set_two_ten);
        set_ten=(TextView) findViewById(R.id.set_ten);
        cancel=(Button) findViewById(R.id.cancel);
        timeProcess=(musicProcess) findViewById(R.id.time_process);
    }

    private void init() {
        Log.e("oncreate","oncreate");
        LinearLayoutManager llm=new LinearLayoutManager(this);
        recyclerView.setLayoutManager(llm);
        recyclerView.setAdapter(musicAdapter);
        isStart.setOnClickListener(this);
        contral.setOnClickListener(this);
        sz.setOnClickListener(this);
        setGreen.setOnClickListener(this);
        setPink.setOnClickListener(this);
        setRed.setOnClickListener(this);
        setPaper.setOnClickListener(this);
        setBlue.setOnClickListener(this);
        set_ten.setOnClickListener(this);
        set_two_ten.setOnClickListener(this);
        set_three_ten.setOnClickListener(this);
        set_four_ten.setOnClickListener(this);
        set_five_ten.setOnClickListener(this);
        set_six_ten.setOnClickListener(this);
        cancel.setOnClickListener(this);
        launchMyserve=new Intent(MainActivity.this,MyserveForSetTime.class);
        Glide.with(this).load(R.drawable.music_img).into(musicImage);

        search_bar.clearFocus();
        search_bar.setFocusable(false);
        search_bar.setOnClickListener(this);
        //设置侧边通知栏点击事件
        setNavInfo();

    }

    private void setNavInfo() {
        nav.setCheckedItem(R.id.theme);
        nav.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                drawerLayout.closeDrawers();
                switch (item.getItemId()){
                    case R.id.theme:
                        setTheme.setVisibility(View.VISIBLE);
                        setTime.setVisibility(View.GONE);
                        sz.setVisibility(View.VISIBLE);
                        break;
                    case R.id.data_time:
                        setTheme.setVisibility(View.GONE);
                        setTime.setVisibility(View.VISIBLE);
                        sz.setVisibility(View.VISIBLE);
                        break;
                    default:
                }
                return false;
            }
        });
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    public void musicList(){
        Uri uri= MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
        ContentResolver contentResolver=getContentResolver();
        Cursor c=contentResolver.query(uri,null,null,null,null);
        list.clear();
        if(c!=null){
            c.moveToFirst();
            while (c.moveToNext()){
                songName=c.getString(c.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
                String[] n=songName.split("-",2);
                songN=n[n.length-1];
                authorN=n[0];
                songId=c.getInt(c.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_ID));
                songPath=c.getString(c.getColumnIndex(MediaStore.Audio.Media.DATA));
                songImg="content://media/external/audio/media/"+songId+"/albumart";
                Log.e("图片地址",songImg);
                songImgPath=Uri.parse(songImg);
                list.add(new Music(songN,authorN,songImgPath,songPath,songId));
            }
        }
        Log.e("歌曲数量",""+list.size());
        musicAdapter=new MusicAdapter(list,MainActivity.this);
        init();
        c.close();
    }

    @Override
    public void onMusicList(String musicPath, Uri songImgPath,String songName,int position) {
        searchContent.setVisibility(View.GONE);

        sendPosition=position;
        if(startMusic==null){
            Intent intent=new Intent(MainActivity.this,PlayServer.class);
            startService(intent);
            bindService(intent,setStartMusic(musicPath),BIND_AUTO_CREATE);
        }else {
            startPlay.player(musicPath);
        }

        displayName.setText(songName);
        isStart.setImageResource(R.drawable.pause);
        Glide.with(this).load(songImgPath).into(musicImage);
        //setNotification(songName,songImgPath);
        sendName=songName;
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();

        try{
            startPlay.clearNotification();
        }catch (NullPointerException e){}
        try{
            unbindService(startMusic);
            Log.e("摧毁","unbindService(startMusic)");
        }catch (Exception e){
            e.printStackTrace();
        }


        if(mediaPlayer!=null){
            mediaPlayer.stop();
            mediaPlayer.release();
        }


        if(exitContent!=null){
            unbindService(exitContent);
            Log.e("摧毁"," unbindService(exitContent)");
        }

        unregisterReceiver(musicChange);
        unregisterReceiver(exit);

        timeProcess.setStart(true);
        SharedPreferences preferences=getSharedPreferences("lastSong",AppCompatActivity.MODE_PRIVATE);
        SharedPreferences.Editor editor=preferences.edit();
        editor.putInt("position",MainActivity.sendPosition);
        editor.putString("songName",list.get(MainActivity.sendPosition).getSongN());
        editor.commit();
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.is_start:
                if(mediaPlayer!=null){
                    if(mediaPlayer.isPlaying()){
                        mediaPlayer.pause();
                        isStart.setImageResource(R.drawable.start);
                        if(!timeProcess.getWait()){
                            timeProcess.setWait(true);
                        }
                    }else {
                        mediaPlayer.start();
                        isStart.setImageResource(R.drawable.pause);
                        if(timeProcess.getWait()){
                            timeProcess.setNotification();
                        }
                    }
                }
                //setNotification(MainActivity.list.get(MainActivity.sendPosition).getSongN(),MainActivity.list.get(MainActivity.sendPosition).getSongImgPath());
                break;
            case R.id.is_love:
                break;
            case R.id.contral:

                Intent intent=new Intent(MainActivity.this,MusicInterface.class);

                //利用bundle专递对象,传递的对象必须实现Serializable接口,比如自定义的getObject对象
                intent.putExtra("songName",sendName);
                intent.putExtra("songPosition",sendPosition);
                startActivity(intent);
                break;
            case R.id.search_bar:
                search_bar.setFocusable(true);
                search_bar.setFocusableInTouchMode(true);
                search_bar.requestFocus();
                search_bar.findFocus();
                search_bar.setCursorVisible(true);
                search_bar.setEnabled(true);
                break;
            case R.id.sz:
                setTheme.setVisibility(View.GONE);
                setTime.setVisibility(View.GONE);
                sz.setVisibility(View.GONE);
                break;
            case R.id.set_green:
                sz.setVisibility(View.GONE);
                setTheme.setVisibility(View.GONE);
                all.setBackgroundResource(R.drawable.background_green);
                drawerLayout.setBackgroundResource(R.drawable.background_green);
                re.setBackgroundResource(R.drawable.background_green);
                backgroundColor=R.drawable.background_green;
                cancel.setBackgroundResource(backgroundColor);
                break;
            case R.id.set_red:
                sz.setVisibility(View.GONE);
                setTheme.setVisibility(View.GONE);
                all.setBackgroundResource(R.drawable.background_red);
                drawerLayout.setBackgroundResource(R.drawable.background_red);
                re.setBackgroundResource(R.drawable.background_red);
                backgroundColor=R.drawable.background_red;
                cancel.setBackgroundResource(backgroundColor);
                break;
            case R.id.set_parper:
                sz.setVisibility(View.GONE);
                setTheme.setVisibility(View.GONE);
                all.setBackgroundResource(R.drawable.background);
                drawerLayout.setBackgroundResource(R.drawable.background);
                re.setBackgroundResource(R.drawable.background);
                backgroundColor=R.drawable.background;
                cancel.setBackgroundResource(backgroundColor);
                break;
            case R.id.set_pink:
                sz.setVisibility(View.GONE);
                setTheme.setVisibility(View.GONE);
                all.setBackgroundResource(R.drawable.background_pink);
                drawerLayout.setBackgroundResource(R.drawable.background_pink);
                re.setBackgroundResource(R.drawable.background_pink);
                backgroundColor=R.drawable.background_pink;
                cancel.setBackgroundResource(backgroundColor);
                break;
            case R.id.set_blue:
                sz.setVisibility(View.GONE);
                setTheme.setVisibility(View.GONE);
                all.setBackgroundResource(R.drawable.background_blue);
                drawerLayout.setBackgroundResource(R.drawable.background_blue);
                re.setBackgroundResource(R.drawable.background_blue);
                backgroundColor=R.drawable.background_blue;
                cancel.setBackgroundResource(backgroundColor);
                break;
            case R.id.set_ten:
                finishTime=600;
                if (exitContent==null){
                    this.bindService(launchMyserve,setConnent(),BIND_AUTO_CREATE);
                }else {
                    myserveTime.setTime(finishTime);
                }
                Toast.makeText(MainActivity.this,"定时已开启,软件将在10分钟后退出",Toast.LENGTH_SHORT).show();
                sz.setVisibility(View.GONE);
                setTime.setVisibility(View.GONE);
                break;
            case R.id.set_two_ten:
                finishTime=1200;
                if (exitContent==null){
                    this.bindService(launchMyserve,setConnent(),BIND_AUTO_CREATE);
                }else {
                    myserveTime.setTime(finishTime);
                }
                Toast.makeText(MainActivity.this,"定时已开启,软件将在20分钟后退出",Toast.LENGTH_SHORT).show();
                sz.setVisibility(View.GONE);
                setTime.setVisibility(View.GONE);
                break;
            case R.id.set_three_ten:
                finishTime=1800;
                if (exitContent==null){
                    this.bindService(launchMyserve,setConnent(),BIND_AUTO_CREATE);
                }else {
                    myserveTime.setTime(finishTime);
                }
                Toast.makeText(MainActivity.this,"定时已开启,软件将在30分钟后退出",Toast.LENGTH_SHORT).show();
                sz.setVisibility(View.GONE);
                setTime.setVisibility(View.GONE);
                break;
            case R.id.set_four_ten:
                finishTime=2400;
                if (exitContent==null){
                    this.bindService(launchMyserve,setConnent(),BIND_AUTO_CREATE);
                }else {
                    myserveTime.setTime(finishTime);
                }
                Toast.makeText(MainActivity.this,"定时已开启,软件将在40分钟后退出",Toast.LENGTH_SHORT).show();
                sz.setVisibility(View.GONE);
                setTime.setVisibility(View.GONE);
                break;
            case R.id.set_five_ten:
                finishTime=3000;
                if (exitContent==null){
                    this.bindService(launchMyserve,setConnent(),BIND_AUTO_CREATE);
                }else {
                    myserveTime.setTime(finishTime);
                }
                Toast.makeText(MainActivity.this,"定时已开启,软件将在50分钟后退出",Toast.LENGTH_SHORT).show();
                sz.setVisibility(View.GONE);
                setTime.setVisibility(View.GONE);
                break;
            case R.id.set_six_ten:
                finishTime=3600;
                if (exitContent==null){
                    this.bindService(launchMyserve,setConnent(),BIND_AUTO_CREATE);
                }else {
                    myserveTime.setTime(finishTime);
                }
                sz.setVisibility(View.GONE);
                setTime.setVisibility(View.GONE);
                break;
            case R.id.cancel:
                myserveTime.setIsStart(false);
                sz.setVisibility(View.GONE);
                setTime.setVisibility(View.GONE);
                break;
            default:
                break;
        }
        if(backgroundColor==0){
            backgroundColor=R.drawable.background;
        }

        //保存主体配置
        SharedPreferences theme=getSharedPreferences("setTheme",AppCompatActivity.MODE_PRIVATE);
        SharedPreferences.Editor editor=theme.edit();
        editor.putInt("setTheme",backgroundColor);
        editor.commit();
    }

    @Override
    public void onBackPressed() {
       if(setTheme.getVisibility()==View.VISIBLE || setTime.getVisibility()==View.VISIBLE){
           setTime.setVisibility(View.GONE);
           setTheme.setVisibility(View.GONE);
           sz.setVisibility(View.GONE);
       }else {
           search_bar.clearFocus();
           search_bar.setFocusable(false);
           if(searchContent.getVisibility()==View.VISIBLE){
               searchContent.setVisibility(View.GONE);
           }else {
               try{
                   if(mediaPlayer.isPlaying()){
                       moveTaskToBack(false);
                   }else {
                       super.onBackPressed();
                   }
               }catch (Exception e){
                   e.printStackTrace();
               }
           }
       }
        super.onBackPressed();
    }


    @Override
    protected void onResume() {
        super.onResume();
        if(timeProcess.getWait()){
            timeProcess.setNotification();
        }
    }


    @Override
    public void searchOnclick(int position, String songPath, Uri songImg, String songN) {
        searchContent.setVisibility(View.GONE);

        sendPosition=position;
        if(startMusic==null){
            Intent intent=new Intent(MainActivity.this,PlayServer.class);
            startService(intent);
            bindService(intent,setStartMusic(songPath),BIND_AUTO_CREATE);
        }else {
            startPlay.player(songPath);
        }


        displayName.setText(songN);
        isStart.setImageResource(R.drawable.pause);
        Glide.with(this).load(songImg).into(musicImage);
        //setNotification(songName,songImgPath);
        sendName=songN;
        songName=sendName;

        sendPosition=position;
    }

    class MusicChange extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
                displayName.setText(intent.getStringExtra("musicName"));
                sendPosition=intent.getIntExtra("musicPosition",0);
                sendName=list.get(sendPosition).getSongN();
                songName=MainActivity.list.get(MainActivity.sendPosition).getSongN();
                //remoteViews.setTextViewText(R.id.no_song_name,songName);
                //setNotification(MainActivity.list.get(MainActivity.sendPosition).getSongN(),MainActivity.list.get(MainActivity.sendPosition).getSongImgPath());
                if(MainActivity.mediaPlayer.isPlaying()){
                    MainActivity.this.isStart.setImageResource(R.drawable.pause);
                }else {
                    MainActivity.this.isStart.setImageResource(R.drawable.start);
                }
        }
    }

    class Exit extends  BroadcastReceiver{
        @Override
        public void onReceive(Context context, Intent intent) {
            Toast.makeText(MainActivity.this,"时间到",Toast.LENGTH_SHORT).show();
            finish();
        }
    }


    public ServiceConnection setConnent(){
        exitContent=new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
                myserveTime= (MyserveForSetTime.Time) iBinder;
                myserveTime.setTime(finishTime);
                myserveTime.startTime();
            }

            @Override
            public void onServiceDisconnected(ComponentName componentName) {
                myserveTime.setIsStart(false);
            }
        };
        return exitContent;
    }

    public ServiceConnection setStartMusic(final String musicPath){
        startMusic=new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
                startPlay=(PlayServer.StartPlay) iBinder;
                startPlay.setContext(MainActivity.this);
                startPlay.player(musicPath);
            }

            @Override
            public void onServiceDisconnected(ComponentName componentName) {
                startPlay.clearNotification();
                Log.e("加载取消绑定","加载取消绑定");
            }
        };
        return startMusic;
    }

    @Override
    protected void onPause() {
        super.onPause();
        if(!timeProcess.getWait()){
            timeProcess.setWait(true);
        }
    }
}
